package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IHomePageContentFragmentModle;
import com.wgts.wgts.mvp.model.IHomePageCurrencyActivityModel;
import com.wgts.wgts.mvp.presenter.IHomePageCurrencyActivityPresenter;
import com.wgts.wgts.mvp.view.IHomePageCurrencyActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.SpecialProductReq;
import com.wgts.wgts.netmodle.resp.ComponentIndextActivityEntity;
import com.wgts.wgts.netmodle.resp.SpecialProductEntity;

import java.util.List;

public class HomePageCurrencyActivityPresenter implements IHomePageCurrencyActivityPresenter {


    private IHomePageCurrencyActivityView view;

    private IHomePageCurrencyActivityModel modle;

    private int activityId;

    public HomePageCurrencyActivityPresenter(IHomePageCurrencyActivityView view, IHomePageCurrencyActivityModel modle) {
        this.view = view;
        this.modle = modle;
    }

    private int pageSize = 20;

    private int pageIndex = 1;

    @Override
    public void initProduct(int activityId) {
        SpecialProductReq req = new SpecialProductReq();
        this.activityId = activityId;
        req.activityId = activityId;
        pageIndex = 1;
        req.pageIndex = pageIndex;
        req.pageSize = pageSize;

        modle.initSpecialResult(req, new HttpResultSubscriber<SpecialProductEntity>() {
            @Override
            public void onSuccess(SpecialProductEntity entity) {
                if(entity.list.size() < entity.pageSize){
                    view.initProductResult(entity, true);
                }else{
                    view.initProductResult(entity, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });

    }

    @Override
    public void initHeader(int parentTypeId) {
        modle.componentIndexActivity(parentTypeId, new HttpResultSubscriber<List<ComponentIndextActivityEntity>>() {
            @Override
            public void onSuccess(List<ComponentIndextActivityEntity> componentIndextActivityEntities) {
                view.initHeader(componentIndextActivityEntities);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void moreProduct() {
        SpecialProductReq req = new SpecialProductReq();
        req.activityId = activityId;
        req.pageSize = pageSize;
        req.pageIndex = pageIndex;

        modle.initSpecialResult(req, new HttpResultSubscriber<SpecialProductEntity>() {
            @Override
            public void onSuccess(SpecialProductEntity entity) {
                if(entity.list.size() < entity.pageSize){
                    view.moreProductResult(entity, true);
                }else{
                    view.moreProductResult(entity, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });

    }
}
